<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<link rel="stylesheet" href="css/bootstrap.min.css">
<link rel="stylesheet" href="css/laypage.css">
<script type="text/javascript" src="js/jquery/jquery.min.js"></script>
<script type="text/javascript" src="js/laypage/laypage.js"></script>
<script type="text/javascript" src="js/layer/layer.js"></script>
<script type="text/javascript" src="js/vue/vue.min.js"></script>
<script type="text/javascript" src="js/jquery.validation/1.14.0/jquery.validate.min.js"></script>

<style>

    input.error{
        border: 1px solid red;
    }

</style>
<body>

<div class="container">
    <form id="addUserForm">
        <br/>
        <div class="form-group">
            <label for="username">用户名称：</label>
            <input type="text" class="form-control" id="username" name="username" placeholder="输入名称">
        </div>
        <div class="form-group">
            <label for="userage">用户年龄：</label>
            <input type="text" class="form-control" id="userage" name="userage" placeholder="输入年龄">
        </div>
        <div class="form-group">
            <label for="pwd">用户名称：</label>
            <input type="text" class="form-control" id="pwd" name="pwd" placeholder="输入密码">
        </div>
        <div class="form-group">
            <label for="time">用户年龄：</label>
            <input type="text" class="form-control" id="time" name="time" placeholder="输入时间">
        </div>
        <div class="form-group">
            <button type="button" id="saveBtn" class="btn btn-success">提交</button>
            <button type="button" id="cancelBtn" class="btn btn-default">取消</button>
        </div>
    </form>
</div>
</body>
</html>
<script>
    var addUser = function () {
        if (!check().form()) {
            return;
        }
        $.ajax({
            type: 'GET',
            dataType: 'json',
            url: '/person/addPerson.do',
            data: {
                name: $("#username").val(),
                age: $("#userage").val(),
                pwd: $("#pwd").val(),
                time: $("#time").val(),
            },
            success: function () {
                $("#cancelBtn").click();
            }
        });

    }


    $("#saveBtn").on("click", function () {
        addUser();
    });

    $("#cancelBtn").on("click", function () {
        var index = parent.layer.getFrameIndex(window.name);
        parent.getUserPageList();
        parent.layer.close(index);
    })


    function check() {
        return $("#addUserForm").validate({
            rules: {
                username: {
                    required: true
                },
                userage: {
                    required: true
                },
                ped: {
                    required: true
                },
                time: {
                    required: true
                }
            },
            messages: {
                username: {
                    required: ""
                },
                userage: {
                    required: ""
                },
                pwd: {
                    required: ""
                },
                time: {
                    required: ""
                }
            }
        })
    }

</script>
